import config from '@/config'
import $api from '@/api/apiList'
import { getLocalColors } from '@/utils'

export const state = () => ({
    currentTime: new Date(),
    weathers: [], // 一周天气列表
    weather: '', // 当日天气
    websiteConfig: '', // 网站信息及首页数据
    asideConfig: '', // 右侧公告数据
    authUser: null, // 用户信息
    fixedComponentQuery: '',
    totalIntegral: 0, // 积分数量
    newMsgNum: 0, // 消息数量
    DetailInfoBooleans: {}, // 网站配置信息  对象
    visitors: '',
    importNoticeToken: false, // 重大通知标识
    importNotice: [], // 重大通知数组
    aboutData: config.about || {}, // 关于我页面数据
    keyWordList: '', // 搜索热词
    themeChanged: false,
    themeConfig: {
    }
})

export const mutations = {
    // 重大通知逐条递减
    setImportNotice (state, data) {
        if (data) {
            state.importNotice = data
        } else {
            state.importNotice.shift()
        }
    },
    setnewMsgNum (state, data) {
        state.newMsgNum = data
    },
    SET_USER (state, user) {
        state.authUser = user
        state.totalIntegral = user.totalIntegral
        state.newMsgNum = user.newMsgNum
    },
    setWebConfig (state, [settingRes, homeRes]) {
        if (settingRes && homeRes && settingRes.success && homeRes.success) {
            settingRes = settingRes.data
            for (const key in settingRes) {
                if (key === 'banner' ||
                    key === 'theme' ||
                    key === 'siteConfig' ||
                    key === 'activity' ||
                    key === 'notice') {
                    if (key !== null) {
                        settingRes[key] = JSON.parse(settingRes[key])
                    } else {
                        settingRes[key] = {}
                    }
                }
            }
            state.websiteConfig = { ...settingRes, ...homeRes.data }
        } else {
            state.websiteConfig = {}
        }
    },
    setAsideData (state, [tagRes, categoryRes]) {
        let asideConfig = {}
        if (categoryRes && categoryRes.success) {
            asideConfig = {
                ...asideConfig,
                categoryList: categoryRes.data[0],
                categoryTotal: categoryRes.data[1]
            }
        }
        if (tagRes && tagRes.success) {
            asideConfig = {
                ...asideConfig,
                tagList: tagRes.data[0],
                tagTotal: tagRes.data[1]
            }
        }
        state.asideConfig = asideConfig
        // console.log(state.asideConfig, 'asideConfig')
    },
    setWeather (state, res) {
        if (res[1].errcode === 100) return
        // state.weather = res[0]
        // state.weathers = res[1].data
        // state.weatherIcons = config.weatherIcons
        // state.weatherIcons.map(el => {
        //     if (state.weather.wea.includes(el.name)) {
        //         state.weather.icon = el.value
        //     }
        // })
        // state.weathers.map(el => {
        //     state.weatherIcons.map(val => {
        //         if (el.wea.includes(val.name)) {
        //             el.icon = val.value
        //         }
        //     })
        // })
        if (res && res[0] && res[0][0]) {
            state.weather = res[0][0]
            state.weathers = res[0]
            state.weatherIcons = config.weatherIcons
            state.weatherIcons.map(el => {
                if (state.weather.weather.includes(el.name)) {
                    state.weather.icon = el.value
                }
            })
            state.weathers.map(el => {
                state.weatherIcons.map(val => {
                    if (el.weather.includes(val.name)) {
                        el.icon = val.value
                    }
                })
            })
            // console.log(state.weathers, 'weathers')
        }
    },
    setKeyWordList (state, res) {
        state.keyWordList = res
    },
    themeChanged (state, data) {
        const { themeChanged, ...others } = data
        state.themeChanged = data.themeChanged
        state.themeConfig = {
            ...state.themeConfig,
            ...others
        }
    }
}

export const actions = {
    async nuxtServerInit ({ commit, dispatch }, { req }) {
        // console.log(req.origin)
        // await dispatch('setAsideData')
    },
    async setAsideData ({ commit, state }, data) {
        if (!state.asideConfig) {
            // console.log(tagRes, categoryRes, 'asideConfig')
            let [tagRes, categoryRes] = await Promise.all([$api.tag.get(), $api.category.get()])
            commit('setAsideData', [tagRes, categoryRes])
            return Promise.resolve({ tagRes, categoryRes })
        }
    },
    login ({ commit }, data) {
        commit('SET_USER', data)
    },
    themeChanged ({ commit, dispatch }, data) {
        commit('themeChanged', data)
    },
    setWebConfig ({ commit, dispatch }, data) {
        commit('setWebConfig', data)
    }
}
